using UnityEngine;

[CreateAssetMenu(menuName = "Animation Curve Clip")]
public class AnimationCurveClip : ScriptableObject
{
	[HideInInspector]
	public int frameRate = 30;

	[HideInInspector]
	public int totalFrames;

	[HideInInspector]
	public float pixelPerUnit = 50f;

	public LayerTimeline[] layers = new LayerTimeline[0];

	public LibrarySprites[] library = new LibrarySprites[0];

	public FrameNameLabel[] frameLabels = new FrameNameLabel[0];

	private int d;

	private int z;

	private int a;

	public float totalTime => LayerTimeline.Round((float)totalFrames / (float)frameRate);

	public int GetIdFrame(string keyframeName)
	{
		for (d = 0; d < frameLabels.Length; d++)
		{
			if (frameLabels[d].name == keyframeName)
			{
				return frameLabels[d].startFrame;
			}
		}
		return 0;
	}

	public int GetIdLastFrame(string keyframeName)
	{
		for (d = 0; d < frameLabels.Length; d++)
		{
			if (frameLabels[d].name == keyframeName)
			{
				frameLabels[d].lastFrame = Mathf.Min(frameLabels[d].lastFrame, totalFrames);
				return (frameLabels[d].startFrame >= frameLabels[d].lastFrame) ? totalFrames : frameLabels[d].lastFrame;
			}
		}
		return totalFrames;
	}

	public Sprite GetLibrarySprite(int layerIndex, int frame)
	{
		int num = 0;
		d = 0;
		while (d < layers[layerIndex].frames.Length && layers[layerIndex].frames[d].frame <= frame)
		{
			num = d;
			d++;
		}
		for (a = 0; a < library.Length; a++)
		{
			if (library[a].name == layers[layerIndex].frames[num].libraryName)
			{
				return library[a].sprites[Mathf.RoundToInt(layers[layerIndex].curveSprite.Evaluate(LayerTimeline.Round((float)frame / (float)frameRate))) % library[a].sprites.Length];
			}
		}
		return null;
	}

	public bool HasKeyframe(string keyframeName)
	{
		for (d = 0; d < frameLabels.Length; d++)
		{
			if (frameLabels[d].name == keyframeName)
			{
				return true;
			}
		}
		return false;
	}

	public void SetFrameRate(int newFrameRate, float newPixelPerUnit)
	{
		for (d = 0; d < layers.Length; d++)
		{
			layers[d].UpdateCurve(newFrameRate, newPixelPerUnit, library);
		}
		frameRate = newFrameRate;
		pixelPerUnit = newPixelPerUnit;
	}

	public void SetKeyframe(LayerTimeline[] newKeyframes)
	{
		layers = newKeyframes;
		for (d = 0; d < layers.Length; d++)
		{
			layers[d].UpdateCurve(frameRate, pixelPerUnit, library);
		}
		totalFrames = 0;
		for (d = 0; d < layers.Length; d++)
		{
			for (a = 0; a < layers[d].frames.Length; a++)
			{
				totalFrames = Mathf.Max(totalFrames, layers[d].frames[a].frame);
			}
		}
	}
}
